package com.hongyuan.venue.ui.home.view_mode;

import android.app.Application;
import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;
import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.mvvmhabitx.bus.RxBus;
import com.hongyuan.mvvmhabitx.bus.RxSubscriptions;
import com.hongyuan.mvvmhabitx.bus.event.SingleLiveEvent;
import com.hongyuan.venue.R;
import com.hongyuan.venue.base.CustomViewModel;
import com.hongyuan.venue.base.ItemViewModel;
import com.hongyuan.venue.data.Constants;
import com.hongyuan.venue.data.MyRepository;
import com.hongyuan.venue.data.rxbusbean.ChangeStoreMessage;
import com.hongyuan.venue.ui.home.activity.StoreListActivity;
import com.hongyuan.venue.ui.home.activity.TrainListActivity;
import com.hongyuan.venue.ui.home.beans.MemberOsBean;
import com.hongyuan.venue.ui.home.beans.TrainBeans;
import com.hongyuan.venue.ui.home.beans.Venue;
import com.hongyuan.venue.ui.person.activity.CouponHubActivity;

import java.util.List;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import me.tatarka.bindingcollectionadapter2.BR;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class FragmentHomeViewModel extends CustomViewModel<MyRepository> {

    private Disposable mSubscription;

    public ObservableField<Venue> entity = new ObservableField<>();

    //给RecyclerView添加ObservableList
    public ObservableList<HomeCouponItemViewModel> couponList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<HomeCouponItemViewModel> couponBinding = ItemBinding.of(BR.viewModel, R.layout.item_home_coupon);

    //给RecyclerView添加ObservableList
    public ObservableList<HomeTrainingItemViewModel> trainList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<HomeTrainingItemViewModel> trainBinding = ItemBinding.of(BR.viewModel, R.layout.item_home_training);

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        //显示销售顾问滚动弹框
        public SingleLiveEvent<List<Venue.GcListBean>> showScheduled = new SingleLiveEvent<>();
    }

    public FragmentHomeViewModel(@NonNull Application application, MyRepository repository) {
        super(application, repository);
    }

    //门店列表
    public BindingCommand goStoreList = new BindingCommand(() -> {
        startActivity(StoreListActivity.class);
    });

    //去领卷中心
    public BindingCommand goCouponHub = new BindingCommand(() -> {
        startActivity(CouponHubActivity.class);
    });

    //去培训课列表
    public BindingCommand goTrainList = new BindingCommand(() -> {
        startActivity(TrainListActivity.class);
    });

    @Override
    public void onCreate() {
        super.onCreate();

        mSubscription = RxBus.getDefault().toObservable(ChangeStoreMessage.class)
                .observeOn(AndroidSchedulers.mainThread()) //回调到主线程更新UI
                .subscribe(progressLoadBean -> {
                    couponList.clear();
                    trainList.clear();

                    //更新下数据
                    //请求门店信息
                    clearParams().setParams("os_id",progressLoadBean.getOs_id())
                    .setParams("os_ids",String.valueOf(progressLoadBean.getOs_id()));
                    requestData(Constants.GYM_DETAIL);

                });
        //将订阅者加入管理站
        RxSubscriptions.add(mSubscription);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();

        //取消订阅，防止内存泄漏
        RxSubscriptions.remove(mSubscription);
    }

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.GET_MEMEBER_OS){
            List<MemberOsBean.ListBean> list = ((MemberOsBean)dataBean).getList();
            if(list != null && list.size() > 0){

                //更改全局os_id
                userToken.setOs_id(String.valueOf(list.get(0).getOs_id()));

                //请求门店信息
                clearParams().setParams("os_id",String.valueOf(list.get(0).getOs_id()));
                requestData(Constants.GYM_DETAIL);

            }
        }

        if(code == Constants.GYM_DETAIL){
            Venue infoBean = (Venue)dataBean;
            entity.set(infoBean);

            uc.showScheduled.setValue(infoBean.getGc_list());

            if(infoBean.getCoupon_list() != null && infoBean.getCoupon_list().size() > 0 ){
                for(int i = 0 ; i < infoBean.getCoupon_list().size() ; i++){
                    HomeCouponItemViewModel couponItemViewModel = new HomeCouponItemViewModel(this,infoBean.getCoupon_list().get(i),i,(infoBean.getCoupon_list().size() - 1));
                    couponList.add(couponItemViewModel);
                }
            }

            if(infoBean.getCourse_train_list() != null && infoBean.getCourse_train_list().size() > 0){
                for(int i = 0 ; i < infoBean.getCourse_train_list().size() ; i++){
                    HomeTrainingItemViewModel itemViewModel = new HomeTrainingItemViewModel(this,infoBean.getCourse_train_list().get(i),i,infoBean.getCourse_train_list().size() - 1);
                    trainList.add(itemViewModel);
                }
            }

        }

        if(code == Constants.GET_COUPON){
            couponList.remove(itemViewModel);
        }
    }

    //当前操作的对象
    public HomeCouponItemViewModel itemViewModel;
    @Override
    public void itemClick(ItemViewModel itemVM) {
        super.itemClick(itemVM);
        this.itemViewModel = (HomeCouponItemViewModel) itemVM;
    }
}
